package com.example.product.controller;


import jakarta.annotation.Resource;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class TestController {

    @Resource
    DiscoveryClient discoveryClient;

    @GetMapping("/test1")
    public void test1() {
        // 获取服务列表
        List<String> services = discoveryClient.getServices();
        for (String service : services) {
            System.out.println(service);
            // 获取每一个服务有多少个实例
            List<ServiceInstance> instances = discoveryClient.getInstances(service);
            for (ServiceInstance instance : instances) {
                System.out.println(instance.getServiceId() + ":" + instance.getHost() + ":" + instance.getPort());
            }

        }

    }
}
